package com.LeeCode;

import java.util.List;

/**
 * 数组列表中的最大距离
 */

public class Code624 {
    public static void main(String[] args) {

    }

    public int maxDistance(List<List<Integer>> arrays) {
        int globalMin = arrays.get(0).get(0), globalMax = arrays.get(0).get(arrays.get(0).size() - 1);
        int ans = 0;

        for (int i = 1; i < arrays.size(); i++) {
            List<Integer> list = arrays.get(i);
            int curMin = list.get(0), curMax = list.get(list.size() - 1);
            // 计算当前数组与之前所有数组的最大距离
            // 当前数组的最大值 - 之前数组的最小值
            ans = Math.max(ans, curMax - globalMin);
            // 之前数组的最大值 - 当前数组的最小值
            ans = Math.max(ans, globalMax - curMin);

            globalMin = Math.min(globalMin, curMin);
            globalMax = Math.max(globalMax, curMax);
        }

        return ans;
    }
}
